package com.zhou.controller;

import cn.hutool.json.JSONUtil;
import com.zhou.bpm.domain.dto.ApproveRequestDto;
import com.zhou.bpm.domain.dto.InstanceRequestDto;
import com.zhou.common.Result;
import com.zhou.common.UserContext;
import com.zhou.test.domain.dto.AddDto;
import com.zhou.test.domain.dto.LoginDto;
import com.zhou.test.entity.SystemUser;
import com.zhou.test.service.ISystemUserService;
import com.zhou.test.service.ITestService;
import com.zhou.util.JwtUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * @Author: zhoujinchuan
 * @Description: TODO（请添加描述）
 * @CreateTime: 2023/10/25 17:20
 */
@RestController
@RequestMapping
@Api(tags = "测试")
public class TestController {

    @Autowired
    private ITestService testService;

    @Autowired
    private ISystemUserService userService;

    @PostMapping("login")
    public Result<String> login(@RequestBody LoginDto dto){
        String userName = dto.getUserName();
        SystemUser user = userService.lambdaQuery().eq(SystemUser::getUserName, userName).one();
        Assert.notNull(user,"账号不存在");
        Assert.isTrue(dto.getPassword().equals(user.getPassword()),"密码错误");
        String token = JwtUtil.createJWT(JSONUtil.toJsonStr(user));
        return Result.success(token);
    }

    @GetMapping("getmine")
    public Result<SystemUser> getmine(){
        SystemUser user = UserContext.getUser();
        return Result.success(user);
    }

    @PostMapping("testAddTitle")
    @ApiOperation("测试创建流程")
    public Result<String> testBegin(@RequestBody @Valid InstanceRequestDto<AddDto> instanceRequestDto) {
        testService.testBegin(instanceRequestDto);
        return Result.ok();

    }

    @PostMapping("testApproveTitle")
    @ApiOperation("测试审批")
    public Result<String> testApproveTitle(@RequestBody @Valid ApproveRequestDto<String> approveRequestDto) {
        testService.testApproveTitle(approveRequestDto);
        return Result.ok();
    }


}
